package com.zjdiepu.www.biz.his;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.his.HisYwBusinessBuyerMapper;
import com.zjdiepu.www.ibiz.his.IHisYwBusinessBuyerService;
import com.zjdiepu.www.model.his.dto.HisYwBusinessBuyer;
import com.zjdiepu.www.model.his.ext.HisYwBusinessBuyerExt;

/**
 * 购车人信息历史记录Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年09月21日 14时40分<br/>
 * @since 1.0<br/>
 */
@Service
public class HisYwBusinessBuyerServiceImpl extends BaseService implements IHisYwBusinessBuyerService {
	public static final Logger logger = LoggerFactory.getLogger(HisYwBusinessBuyerServiceImpl.class);

	@Autowired
	private HisYwBusinessBuyerMapper hisYwBusinessBuyerMapper;
	
	@Override
	public void saveUpdateAndDelete(HisYwBusinessBuyer hisYwBusinessBuyer, OperateType operType) {
		switch (operType) {
		case add:
			hisYwBusinessBuyer.initInsertData(hisYwBusinessBuyer.getModifier(), hisYwBusinessBuyer.getModifierName());
			hisYwBusinessBuyerMapper.insert(hisYwBusinessBuyer);
			break;
		case edit:
			hisYwBusinessBuyer.initUpdateData(hisYwBusinessBuyer.getModifier(), hisYwBusinessBuyer.getModifierName());
			// 影响的记录数
			int updateCount = hisYwBusinessBuyerMapper.updateByPrimaryKeySelective(hisYwBusinessBuyer);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			hisYwBusinessBuyer.initDeleteData(hisYwBusinessBuyer.getModifier(), hisYwBusinessBuyer.getModifierName());
			// 影响的记录数
			int deleteCount = hisYwBusinessBuyerMapper.updateByPrimaryKeySelective(hisYwBusinessBuyer);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<HisYwBusinessBuyer> selectByExt(HisYwBusinessBuyerExt hisYwBusinessBuyerExt) {
		return hisYwBusinessBuyerMapper.selectByExt(hisYwBusinessBuyerExt);
	}
	
	@Override
	public int countByExt(HisYwBusinessBuyerExt hisYwBusinessBuyerExt){
		return hisYwBusinessBuyerMapper.countByExt(hisYwBusinessBuyerExt);
	}
	
	@Override
	public HisYwBusinessBuyer selectById(String id) {
		return hisYwBusinessBuyerMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			hisYwBusinessBuyerMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(HisYwBusinessBuyer hisYwBusinessBuyer) {
		return hisYwBusinessBuyerMapper.countRefined(hisYwBusinessBuyer);
	}

	@Override
	public HisYwBusinessBuyer selectRefined(HisYwBusinessBuyer hisYwBusinessBuyer) {
		return hisYwBusinessBuyerMapper.selectRefined(hisYwBusinessBuyer);
	}
	
	@Override
	public int deletedRefined(HisYwBusinessBuyer hisYwBusinessBuyer) {
		return hisYwBusinessBuyerMapper.deletedRefined(hisYwBusinessBuyer);
	}

	@Override
	public boolean repeat(HisYwBusinessBuyer hisYwBusinessBuyer) {
		int count = countRefined(hisYwBusinessBuyer);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(hisYwBusinessBuyer.getId())) {
			return true;
		}
		HisYwBusinessBuyer dbItem = selectRefined(hisYwBusinessBuyer);
		if(null == dbItem) {
			return false;
		}
		return !hisYwBusinessBuyer.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return hisYwBusinessBuyerMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<HisYwBusinessBuyer> hisYwBusinessBuyers) {
		return hisYwBusinessBuyerMapper.insertBatch(hisYwBusinessBuyers);
	}
}
